package com.myedusys.controller;

import com.myedusys.utils.AliOssUtil;
import Result.Result;
import com.myedusys.constant.UserConstant;
import com.myedusys.context.BaseContext;
import com.myedusys.service.UserService;
import dto.UpdatePwdDto;
import entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import vo.UserLoginVo;

import javax.validation.constraints.Pattern;
import java.io.IOException;
import java.util.UUID;

@RestController
@Slf4j
@Api(tags = "用户接口")
@RequestMapping("/user")
@Validated
public class UserController {

    @Autowired
    UserService userService;

    @Autowired
    AliOssUtil aliOssUtil;

    @PostMapping("/register")
    @ApiOperation("用户注册")
    public Result register(String username, String password) {
        //查询用户是否存在
        User user = userService.findByUserName(username);
        if (user != null) {
            return Result.error(UserConstant.USER_ADD_IS_EXIST);
        }

        //注册
        userService.register(username, password);
        return Result.success(UserConstant.USER_ADD_SUCCESS);
    }

    //public Result login(@Pattern(regexp = "^\\${5,16}$") String username, @Pattern(regexp = "^\\${5,16}$") String password)
    @PostMapping("/login")
    public Result<UserLoginVo> login(String username, String password) {

        return Result.success(userService.login(username, password));

    }


    @GetMapping("/userInfo")
    @ApiOperation("查询用户信息")
    public Result<User> userInfo(){
        //根据当前用户
        Long id = BaseContext.getCurrentId();
        return Result.success(userService.findById(id));
    }


    @ApiOperation("更新用户信息")
    @PutMapping("/update")
    public Result update(@RequestBody User user){
        userService.update(user);
        return Result.success(UserConstant.USER_UPDATE_SUCCESS);
    }

    @ApiOperation("更新用户头像")
    @PatchMapping("/updateAvatar")
    public Result updateAvatar(@RequestParam String avatarUrl){
        userService.updateAvatar(avatarUrl);
        return Result.success(UserConstant.USER_UPLOAD_SUCCESS);
    }

    /**
     * 文件上传
     * @param file
     * @return
     */
    @PostMapping("/upload")
    @ApiOperation("文件上传")
    public Result<String> upload(MultipartFile file){
        log.info("文件上传：{}",file);

        try {
            //原始文件名
            String originalFilename = file.getOriginalFilename();
            //截取原始文件名的后缀   dfdfdf.png
            String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
            //构造新文件名称
            String objectName = UUID.randomUUID().toString() + extension;

            //文件的请求路径
            String filePath = aliOssUtil.upload(file.getBytes(), objectName);
            return Result.success(filePath);
        } catch (IOException e) {
            log.error("文件上传失败：{}", e);
        }

        return Result.error(UserConstant.USER_UPLOAD_ERROR);
    }

    @ApiOperation("更新密码")
    @PatchMapping("/updatePwd")
    public Result<String> updatePwd(@RequestBody  UpdatePwdDto updatePwdDto){
        userService.updatePwd(updatePwdDto);
        return Result.success(UserConstant.USER_UPDATE_PWD_SUCCESS);
    }



}
